from sqlalchemy import Column, INTEGER
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_dm import VARCHAR2, NUMBER

# 创建对象的基类
Base = declarative_base()

# 风险数据表
class RiskTitle(Base):
    # 模式名
    __table_args__ = {'schema': 'ESUNTEST'}
    # 表名
    __tablename__ = 'HX_BI_FX_TITLE'
    # 表结构
    ID = Column(INTEGER, primary_key=True, autoincrement=True)
    YEAR = Column(VARCHAR2(36))
    STARTMONTH = Column(VARCHAR2(36))
    ENDMONTH = Column(VARCHAR2(36))
    FXLX = Column(VARCHAR2(36))
    GSMC = Column(VARCHAR2(128))
    FKJE = Column(NUMBER)
    COUNT = Column(NUMBER)
    GS = Column(VARCHAR2(128))

    @classmethod
    def from_dict(cls, data):
        return cls(
            ID=int(data.get('ID')) if data.get('ID') is not None else 0,
            YEAR=data.get('YEAR'),
            STARTMONTH=data.get('STARTMONTH'),
            ENDMONTH=data.get('ENDMONTH'),
            FXLX=data.get('FXLX'),
            GSMC=data.get('GSMC'),
            FKJE=data.get('FKJE'),
            COUNT=data.get('COUNT'),
            GS=data.get('GS')
        )

    def to_dict(self):
        return {
            'ID': self.ID,
            'GSMC': self.GSMC,
            'YEAR': self.YEAR,
            'STARTMONTH': self.STARTMONTH,
            'ENDMONTH': self.ENDMONTH,
            'FXLX': self.FXLX,
            'FKJE': self.FKJE,
            'COUNT': self.COUNT,
            'GS': self.GS,
        }
